import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:jyofservice/bean/check_list_entity.dart';
import 'package:jyofservice/jicha/JiChaDetail.dart';
import 'package:jyofservice/jicha/LevelTree.dart';
import 'package:jyofservice/util/CallRecordNotification.dart';
import 'package:jyofservice/util/Constants.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:dio/dio.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:shared_preferences/shared_preferences.dart';

class JiChaGuanLiPage extends StatefulWidget {
  @override
  _JiChaGuanLiPageState createState() => _JiChaGuanLiPageState();
}

class _JiChaGuanLiPageState extends State<JiChaGuanLiPage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  ScrollController scrollController = ScrollController();
  int page = 1;
  bool isloading = true;
  Color colortodob = Colors.blue;
  Color colorcheckb = Color.fromRGBO(188, 223, 251, 1);
  Color colornormalb = Color.fromRGBO(188, 223, 251, 1);
  Color colorblackb = Color.fromRGBO(188, 223, 251, 1);
  Color colortodot = Colors.white;
  Color colorcheckt = Colors.blue;
  Color colornormalt = Colors.blue;
  Color colorblackt = Colors.blue;
  int index = 0;
  List<CheckListEntity?> checklist = [];
  StreamSubscription? subscriptionType = null;
  String? wherestr = "xq_code=&lh=&dy=";
  bool iszg = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initData();
    subscriptionType =
        Constants.eventBus.on<CallRecordNotification>().listen((event) {
      print("event.index==${event.wherestr}");
      wherestr = event.wherestr;
      getCheckList();
      if (mounted) {
        setState(() {});
      }
    });

    subscriptionType!.resume();
  }
  Future initData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? roleName = sharedPreferences.getString("rolename");
    if (roleName == "稽查主管") {
      iszg = true;
    } else {
      iszg = false;
    }
    if(mounted) {
      setState(() {
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFF4F4F4),
      key: _scaffoldKey,
      appBar: AppBar(
        centerTitle: true,
        title: Text(
            '稽查管理', //FluroConvertUtils.fluroCnParamsDecode(widget.title),
            style: TextStyle(
                color: Colors.white, fontSize: ScreenUtil().setSp(40))),
        leading: IconButton(icon: Icon(Icons.arrow_back_ios),onPressed: (){

          Navigator.pop(context);

        },),
      ),
      body: Stack(
        children: <Widget>[
          Row(
            children: <Widget>[
              iszg?getLeftJczgWidget():getLeftWidget(),
              checklist.length == 0
                  ? Expanded(
                      flex: 1,
                      child: Center(
                        child: Image.asset("images/nodata.png"),
                      ))
                  : Expanded(
                      flex: 1,
                      child: ListView.builder(
                        itemBuilder: (context, item) {
                          return listitem(context, item);
                        },
                        controller: scrollController,
                        itemCount: checklist.length,
                      ),
                    )
            ],
          ),
          Offstage(
            offstage: isloading,
            child: SpinKitFadingCircle(
              color: Colors.blue,
              size: 50.0,
            ),
          )
        ],
      ),
      endDrawer: LevelTree(),
    );
  }

  Widget listitem(BuildContext context, int i) {
    return GestureDetector(
      onTap: (){
        Navigator.push(context, new MaterialPageRoute(builder: (context) {
          return JiChaDetail(
            checkListEntity: checklist[i],
            state: index,
          );
        })).then((value){
          getCheckList();
        });
      },
      child: Container(
        margin: EdgeInsets.all(5),
        padding: EdgeInsets.all(5),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "images/mine.png",
                  width: ScreenUtil().setWidth(60),
                ),
                Container(
                  margin: EdgeInsets.only(left: 5),
                  alignment: Alignment.centerLeft,
                  child: Column(
                    children: [
                      Text(
                        checklist[i]!.ybbm!,
                        style: TextStyle(
                            color: Colors.black87,
                            fontSize: ScreenUtil().setSp(35)),
                      ),
                      Text(
                        checklist[i]!.yhmc!,
                        style: TextStyle(
                            color: Colors.grey, fontSize: ScreenUtil().setSp(30)),
                      )
                    ],
                  ),
                ),
              ],
            ),
            Container(
              color: Colors.grey,
              height: ScreenUtil().setHeight(1),
            ),
            Row(
              children: [
                Expanded(child: Column(
                  children: [
                    Row(
                      children: <Widget>[
                        Text(
                          '用户类别：',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(30), color: Colors.black87),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.fromLTRB(5, 5, 0, 0),
                            child: Text(
                              checklist[i]!.yhlb!,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30),
                                  color: Colors.black87),
                            ),
                          ),
                        )
                      ],
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          '用暖状态：',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(30), color: Colors.black87),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.fromLTRB(5, 5, 0, 0),
                            child: Text(
                              checklist[i]!.ynzt!,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30),
                                  color: Colors.black87),
                            ),
                          ),
                        )
                      ],
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          '是否VIP：',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(30), color: Colors.black87),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.fromLTRB(5, 5, 0, 0),
                            child: Text(
                              checklist[i]!.isvip!,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30),
                                  color: Colors.black87),
                            ),
                          ),
                        )
                      ],
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          '收费方式：',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(30), color: Colors.black87),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.fromLTRB(5, 5, 0, 0),
                            child: Text(
                              checklist[i]!.sffs!,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(30),
                                  color: Colors.black87),
                            ),
                          ),
                        )
                      ],
                    ),
                  ],
                ))
                ,
                Image.asset(checklist[i]!.jfzt=="1"?'images/yjf.png':'images/no_pay.png',width: ScreenUtil().setWidth(100),)
              ],
            )
           
          ],
        ),
      ),
    )
      ;
  }

  Widget getLeftJczgWidget(){
    return Container(
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().screenHeight,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colortodob = Colors.blue;
                colortodot = Colors.white;
                colorcheckb = Color.fromRGBO(188, 223, 251, 1);
                colorcheckt = Colors.blue;
                colornormalb = Color.fromRGBO(188, 223, 251, 1);
                colornormalt = Colors.blue;
                colorblackb = Color.fromRGBO(188, 223, 251, 1);
                colorblackt = Colors.blue;
                index = 0;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colortodob,
                child: Center(
                  child: Text(
                    getTypeName("待稽查"),
                    style: TextStyle(
                        color: colortodot,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: ScreenUtil().setHeight(2),
            color: Colors.white,
          ),
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colorcheckb = Colors.blue;
                colorcheckt = Colors.white;
                colortodob = Color.fromRGBO(188, 223, 251, 1);
                colortodot = Colors.blue;
                colornormalb = Color.fromRGBO(188, 223, 251, 1);
                colornormalt = Colors.blue;
                colorblackb = Color.fromRGBO(188, 223, 251, 1);
                colorblackt = Colors.blue;
                index = -100;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colorcheckb,
                child: Center(
                  child: Text(
                    getTypeName("待复核"),
                    style: TextStyle(
                        color: colorcheckt,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(100),
            height: ScreenUtil().setHeight(2),
            color: Colors.white,
          ),
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colorblackb = Colors.blue;
                colorblackt = Colors.white;
                colortodob = Color.fromRGBO(188, 223, 251, 1);
                colortodot = Colors.blue;
                colornormalb = Color.fromRGBO(188, 223, 251, 1);
                colornormalt = Colors.blue;
                colorcheckb = Color.fromRGBO(188, 223, 251, 1);
                colorcheckt = Colors.blue;
                index = -200;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colorblackb,
                child: Center(
                  child: Text(
                    getTypeName("黑名单"),
                    style: TextStyle(
                        color: colorblackt,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(100),
            height: ScreenUtil().setHeight(2),
            color: Colors.white,
          ),
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colornormalb = Colors.blue;
                colornormalt = Colors.white;
                colortodob = Color.fromRGBO(188, 223, 251, 1);
                colortodot = Colors.blue;
                colorblackb = Color.fromRGBO(188, 223, 251, 1);
                colorblackt = Colors.blue;
                colorcheckb = Color.fromRGBO(188, 223, 251, 1);
                colorcheckt = Colors.blue;
                index = 100;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colornormalb,
                child: Center(
                  child: Text(
                    getTypeName("正常"),
                    style: TextStyle(
                        color: colornormalt,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget getLeftWidget() {
    return Container(
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().screenHeight,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colortodob = Colors.blue;
                colortodot = Colors.white;

                colornormalb = Color.fromRGBO(188, 223, 251, 1);
                colornormalt = Colors.blue;
                index = 0;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colortodob,
                child: Center(
                  child: Text(
                    getTypeName("待稽查"),
                    style: TextStyle(
                        color: colortodot,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: ScreenUtil().setHeight(2),
            color: Colors.white,
          ),
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                colornormalb = Colors.blue;
                colornormalt = Colors.white;
                colortodob = Color.fromRGBO(188, 223, 251, 1);
                colortodot = Colors.blue;
                index = 100;
                checklist.clear();
                getCheckList();
              },
              child: Container(
                color: colornormalb,
                child: Center(
                  child: Text(
                    getTypeName("正常"),
                    style: TextStyle(
                        color: colornormalt,
                        fontSize: ScreenUtil().setSp(30),
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  String getTypeName(String typeName) {
    String result = "";

    for (int i = 0; i < typeName.length; i++) {
      if (i != typeName.length - 1) {
        String str = typeName.substring(i, i + 1);
        result += str + "\n";
      } else {
        String str = typeName.substring(i, i + 1);
        result += str;
      }
    }
    return result;
  }

  Future getCheckList() async {
    checklist.clear();
    if (mounted) {
      setState(() {
        isloading = false;
      });
    }
    Response response;
    if(index==0||index==-100) {
      response = await Dio().get(WebConfig().jichaUrl +
          "method=get_yb&" +
          wherestr! +
          "&jfzt=&jczt=" +
          index.toString()+"&yhpj=");
    }else if(index==-200){
      response = await Dio().get(WebConfig().jichaUrl +
          "method=get_yb&" +
          wherestr! +
          "&jfzt=&jczt=&yhpj=黑名单");
    }else{
      response = await Dio().get(WebConfig().jichaUrl +
          "method=get_yb&" +
          wherestr! +
          "&jfzt=&jczt=&yhpj=正常");
    }

    print(response.requestOptions.path);
    for (int i = 0; i < response.data.length; i++) {
      CheckListEntity? checkListEntity =
          CheckListEntity().fromJson(response.data[i]);
      checklist.add(checkListEntity);
    }

    setState(() {
      isloading = true;
    });
    return response.data;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    subscriptionType!.cancel();
  }
}
